package com.zhiteng.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhiteng.entity.Assignee;
import com.zhiteng.entity.Procdef;
import com.zhiteng.entity.Teacher;
import com.zhiteng.service.AssigneeService;
import com.zhiteng.service.ProcdefService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.ZTResult;
import com.zhiteng.vo.ProcdefVo;

@RestController
@RequestMapping("/Assignee")
public class AssigneeController {

	@Autowired
	private TeacherService TeacherServiceImpl;

	@Autowired
	private AssigneeService AssigneeServiceImpl;

	@Autowired
	private ProcdefService ProcdefServiceImpl;

	/**
	 * 保存审批人
	 * 
	 * @param name
	 * @param role
	 * @param telephone
	 * @return
	 */
	@RequestMapping("/saveAssignee")
	@ResponseBody
	public ZTResult<Void> saveAssignee(@RequestParam("name") String name, @RequestParam("role") String role,
			@RequestParam("telephone") String telephone, @RequestParam("schoolId") String schoolId) {

		List<Teacher> list = TeacherServiceImpl.queryTeacherByNameAndTelephone(name, telephone);
		if (list.size() != 1) {
			return ZTResult.result(1, "保存失败,名字和电话号码不唯一或者数据库后台没有这个人");
		}
		for (Teacher teacher : list) {
			Assignee assignee = new Assignee();
			assignee.setName(name);
			assignee.setRole(role);
			assignee.setTelephone(telephone);
			assignee.setSchoolName(teacher.getSchool_name());
			assignee.setSchoolId(schoolId);
			assignee.setId(teacher.getUserid());
			assignee.setDel_flag(1);	//	删除标志：0-删除   1-未删除
			int i = AssigneeServiceImpl.saveAssignee(assignee);
			if (i == 1) {
				return ZTResult.result(0, "保存成功");
			}
		}
		return null;
	}

	/**
	 * 通过学校的id查询审批流程
	 * 
	 * @param schoolId
	 * @return
	 */
	@RequestMapping("/queryProcdefBySchoolId")
	@ResponseBody
	public ZTResult<Object> queryProcdefBySchoolId(@RequestParam("schoolId") String schoolId,
			@RequestParam("type") int type) {
		if (StringUtils.isAnyBlank(schoolId, type+"")) {
			return ZTResult.result(1, "查询失败，请检查参数");
		}
		List<Procdef> procdefList = ProcdefServiceImpl.queryProcdefBySchoolIdAndType(schoolId, type);
		List<ProcdefVo> resultList = new ArrayList<>();
		if(procdefList!=null && !procdefList.isEmpty()) {
			for (Procdef procdef : procdefList) {
				ProcdefVo procdefVo = new ProcdefVo();
				procdefVo.setSequence(procdef.getSequence());
				procdefVo.setHours(procdef.getHours());
				procdefVo.setSchool_id(procdef.getSchool_id());
				procdefVo.setType(procdef.getType());
				Assignee assignee = AssigneeServiceImpl.queryAssigneeId(procdef.getAssignee_id());
				if(assignee!=null) {
					procdefVo.setAssignee_id(assignee.getId());
					procdefVo.setAssigneeName(assignee.getName());
					procdefVo.setRole(assignee.getRole());
					procdefVo.setTelephone(assignee.getTelephone());
					resultList.add(procdefVo);
				}else {
					return ZTResult.result(1, "id为"+procdef.getAssignee_id()+"的审批人不存在");
				}
				
			}
		}

		return ZTResult.result(0, "查询审批流程定义表成功", resultList);
	}

	/**
	 * 通过学校的名字查询所有的审批人
	 * 
	 * @param schoolName
	 * @return
	 */
	@RequestMapping("/queryAssigneeBySchoolId")
	@ResponseBody
	public ZTResult<List<Assignee>> queryAssigneeBySchoolName(@RequestParam("schoolId") String schoolId) {

		List<Assignee> lsits = AssigneeServiceImpl.queryAssigneeBySchoolId(schoolId);
		if (lsits.size() == 0 || lsits.isEmpty()) {
			return ZTResult.result(1, "查询审批人失败");
		}
		return ZTResult.result(0, "查询审批人成功", lsits);
	}
	
	/**
	 * 
	* @Title: deleteAssignee  
	* @Description: 批量删除审批人
	* @param assigneeIds
	* @return ZTResult<Void>   
	* @throws
	 */
	@RequestMapping("/deleteAssigneeBatch")
	@ResponseBody
	public ZTResult<Void> deleteAssigneeBatch(@RequestParam("assigneeIds")String[] assigneeIds) {

		int count = AssigneeServiceImpl.deleteAssigneeBatch(assigneeIds);
		if (count==assigneeIds.length) {
			return ZTResult.result(0, "成功删除"+count+"个审批人");
		}
		return ZTResult.result(1, count+"个审批人删除成功，"+(assigneeIds.length-count)+"个审批人删除失败");
	}
	
	
	

//	/**
//	 * 保存审批的流程到审批流程定义表
//	 * 
//	 * @param assigneeAndSchoolIdAndtype
//	 * @return
//	 */
//	@RequestMapping("/saveProcdef")
//	@ResponseBody
//	public ZTResult<Void> saveProcdef(Procdef Procdef) {
//
//		try {
//			int i = ProcdefServiceImpl.deleteProcdefBySchoolId(Procdef.getSchool_id());
//			List<Assignee> list = assigneeAndSchoolIdAndtype.getAssignees();
//
//			for (int j = 0; j < list.size(); j++) {
//				Assignee assignee = list.get(j);
//				Procdef procdef = new Procdef();
//				procdef.setSchool_id(assigneeAndSchoolIdAndtype.getSchoolId());
//				procdef.setSchool_name(assignee.getSchoolName());
//				procdef.setAssignee_id(assignee.getId());
//				procdef.setType(assigneeAndSchoolIdAndtype.getType());
//				procdef.setHours(hours);
//				procdef.setSequence(j + 1);
//
//				ProcdefServiceImpl.saveProcdef(procdef);
//			}
//			return ZTResult.result(0, "保存成功");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return ZTResult.result(1, "保存失败");
//		}
//	}

}
